//
//  KFDVIPServiceViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/9/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDVIPServiceViewController.h"
#import "KFDAccountManager.h"
#import "UIAlertController+KFD.h"
#import "KFDInjectionViewController.h"
#import "KFDUIConfigurator.h"

@interface KFDVIPServiceViewController ()

@end

@implementation KFDVIPServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* configurator=[KFDUIConfigurator configuratorForViewController:self];
    [configurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"VIP服务";
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//---------------------------------------------------------------------
#pragma mark - Table view delegate
//---------------------------------------------------------------------
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==2&&indexPath.row==0) {
        KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
        if (accountManager.isLogin) {
            if(accountManager.isVIPQualified){
                UIAlertController* alertController=[UIAlertController alertControllerWithTitle:nil message:@"您的账号满足VIP用户的申请条件，确定要升级吗？" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction* cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
                [alertController addAction:cancelAction];
                UIAlertAction* confirmAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {
                    [accountManager applyForVIPWithSuccessHandler:^{
                        UIAlertController* alertController=[UIAlertController alertControllerWithTitle:nil message:@"升级成功！" preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction* confirmAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                        [alertController addAction:confirmAction];
                        [self presentViewController:alertController animated:YES completion:nil];
                    } failureHandler:^(KFDResponseBody *response) {
                        UIAlertController* alertController=[UIAlertController alertControllerWithTitle:nil message:@"升级失败！" preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction* confirmAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                        [alertController addAction:confirmAction];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }];
                }];
                [alertController addAction:confirmAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }else{
                UIAlertController* alertController=[UIAlertController alertControllerWithTitle:nil message:@"您的账号资金不足，请先行注资后再申请VIP" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction* cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
                [alertController addAction:cancelAction];
                UIAlertAction* confirmAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {
                    KFDInjectionViewController* injectionVC=[[KFDInjectionViewController alloc] init];
                    [self.navigationController pushViewController:injectionVC animated:YES];
                }];
                [alertController addAction:confirmAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
            
        }else{
            [UIAlertController presentLoginRegisterAlertControllerWithMessage:@"对不起，您尚未登录系统，请登录账号" byViewController:self];
        }
    }
}
//---------------------------------------------------------------------

@end
